//费用管理分布式键值存储类
import distributedKVStore from '@ohos.data.distributedKVStore';
import AlbumData from "../Bean/AlbumData"
let kvManager: distributedKVStore.KVManager;
export default class ExpenseStore{
  kvManager: distributedKVStore.KVManager;
  kvStore: distributedKVStore.SingleKVStore | null;
  constructor(context) {
    const kvManagerConfig: distributedKVStore.KVManagerConfig = {
      context: context,
      bundleName: 'top.myapplication',
    }
    this.kvManager = distributedKVStore.createKVManager(kvManagerConfig);
    const options: distributedKVStore.Options = {
      createIfMissing: true,
      encrypt: false,
      backup: false,
      autoSync: true,
      kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
      securityLevel: distributedKVStore.SecurityLevel.S2,
    };
    this.kvManager.getKVStore('album', options, (err, store: distributedKVStore.SingleKVStore) => {
      if (err) {
        console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`);
        return;
      }
      console.info("Succeeded in getting KVStore");
      this.kvStore = store;
    });
  }
  putData(key,value){
    return new Promise((resolve,reject)=>{
      this.kvStore.put(key,value).then(()=>{
        resolve(1)
      })
    })
  }
  getData(){
    return new Promise((resolve,reject)=>{
      this.kvStore.getEntries('album_',(err, entries)=> {
        if (err != undefined) {
          console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`);
          return;
        }
        console.info('Succeeded in getting Entries');
        console.info(`entries.length: ${entries.length}`);
        console.info(`entries[0]: ${JSON.stringify(entries)}`);
        let albumData=[]
        for(const entry of entries){
          // console.log(JSON.stringify(entry))
          let item=JSON.parse(entry.value.value as string)
          albumData.push(item)
        }
        // console.log(JSON.stringify(expenseData)+"expenseData:")
        resolve(albumData)
      });
    })
  }
  getDataAsRecordID(recordID:number){
    return new Promise((resolve,reject)=>{
      this.kvStore.getEntries('album_',(err, entries)=> {
        if (err != undefined) {
          console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`);
          return;
        }
        console.info('Succeeded in getting Entries');
        console.info(`entries.length: ${entries.length}`);
        console.info(`entries[0]: ${JSON.stringify(entries)}`);
        let albumDataList:Array<AlbumData>=[]
        for(const entry of entries){
          // console.log(JSON.stringify(entry))
          let item:AlbumData=JSON.parse(entry.value.value as string)
          if(item.recordID===recordID){
            albumDataList.push(item)
          }

        }
        // console.log(JSON.stringify(expenseData)+"expenseData:")
        resolve(albumDataList)
      });
    })
  }
  deleteData(key:string){
    return new Promise((resolve,reject)=>{
      this.kvStore.delete(key,(err,data)=>{
        resolve(data)
      })
    })



  }
  close(){
    this.kvManager.closeKVStore('top.myapplication', 'expense').then(() => {
      console.info('Succeeded in closing KVStore');
    }).catch((err) => {
      console.error(`Failed to close KVStore.code is ${err.code},message is ${err.message}`);
    });
  }
}